package com.kele.controller;

import com.kele.entity.User;
import com.kele.reposity.impl.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * @Version 2019
 * @Author:kele
 * @Date:2021/8/8
 * @Content:
 * @Description：
 */
@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    /**
     * 定义port，拿到当前服务的端口号
     */
    @Value("${server.port}")
    private String port;

    @GetMapping("/findAll")
    public Collection<User> findAll(){

        return userRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public User findById(@PathVariable("id") Integer id){

        return userRepository.findById(id);
    }

    @PostMapping("/save")
    public void  save(@RequestBody User user){
        userRepository.addOrUpdate(user);
    }

    @PutMapping("/update")
    public void  update(@RequestBody User user){
        userRepository.addOrUpdate(user);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") Integer id){
        userRepository.deleteById(id);
    }

    /**
     * 返回当前服务的端口号，测试负载均衡
     */
    @GetMapping("/index")
    public String index() {
        return "当前端口是" + this.port;
    }
    public void test(){
        System.out.println("hello world");
    }

}
